package com.ktz.shoping.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ktz.shoping.bean.UserBean;
import com.ktz.shoping.dao.UserDao;
import com.ktz.shoping.dao.UserDaoFactary;
import com.ktz.shoping.db.DBDriver;
import com.ktz.shoping.db.DBDriverFactary;
import com.ktz.shoping.myutils.MyUtil;

/**
 * 登录验证的servlet
 */
@WebServlet(urlPatterns = "/LoginServlet", name = "LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserBean user =  MyUtil.convertRequestParameter2Bean(request, UserBean.class,null,null);
		UserDao ld = UserDaoFactary.getUserDaoInstance();
		if (ld.isLogined(user)) {
			HttpSession session = request.getSession();
			session.setAttribute("username", user.getUsername());
			request.getRequestDispatcher("admin/home.jsp").forward(request, response);
		} else {
			response.sendRedirect("admin/login.jsp");
		}
	}

}
